# -*- coding: utf-8 -*-
"""
Created on Sun Nov 24 20:58:18 2013

@author: Casa
"""
import math
#from numba import jit

#@jit('i4(i4[:])')
def code(data):
    '''FDR coding'''
    __m={}#dictionary
    size=0
    i=0
    while i<data.size:
        k=i
        while k<data.size and data[k]!=1:
            k+=1
        n=k-i
        if not __m.has_key(n):
            group=int(math.ceil(math.log(n+3)/math.log(2)-1))
            l=group*2
            __m[n]=l
        size+=__m[n]
        i=k+1
    return size

def FDR(data):
    '''
    turn the testset into one string and apply FDR coding
    '''
    return code(data.reshape(-1))
#    import cfunc
#    return cfunc.FDR(data.copy())
    
if __name__=='__main__':
    from time import clock
    from testset import testset
    t0=clock()    
    ts=testset('F:/work/vlsi-test/TestDataCompression/circutes/s5378')
    print(ts.name)
    n=FDR(ts.data)
    print ('%.2f'%((1-n*1.0/ts.data.size)*100))
    print (clock()-t0)